package cn.bone.mqtt.demo03;

import com.alibaba.fastjson.JSON;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by MD-01 on 2017/4/24.
 */
public class ServerPush {

    private static Logger logger = LoggerFactory.getLogger(ServerPush.class);

    public static void main(String[] args) throws Exception {
        //String host = "tcp://127.0.0.1:61613";
        //String host = "tcp://127.0.0.1:1883";
        String host = "tcp://172.16.252.234:1883";
        MqttClient client = new MqttClient(host,"server-push",
                new MqttDefaultFilePersistence("d:/mqtt"));
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName("admin");
        options.setPassword("password".toCharArray());
        options.setCleanSession(false);
        //设置自动重新连接
        options.setAutomaticReconnect(true);
        client.setCallback(new DemoCallBack());
        client.connect(options);
        //client.publish("topic/client-02","02,你好".getBytes("UTF-8"),2,true);
        //client.publish("topic/all","大家好.".getBytes("UTF-8"),2,true);
        //client.publish("topic/all","大家好,第三条消息.".getBytes("UTF-8"),2,true);
        //client.publish("topic/all","大家好,第8条消息.".getBytes("UTF-8"),2,true);

        String topicStr = "topic/all";
        String payloadStr = "大家好,第12条消息.";
        MqttTopic topic = client.getTopic(topicStr);
        MqttDeliveryToken deliveryToken = topic.publish(
                payloadStr.getBytes("UTF-8"),
                2,
                true);
        deliveryToken.waitForCompletion();
        client.disconnect();
        logger.info("消息发送成功");




        /*deliveryToken.setActionCallback(new IMqttActionListener(){

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                logger.info("onSuccess");
                MqttWireMessage response = asyncActionToken.getResponse();
                try {
                    logger.info(new String(response.getPayload(),"UTF-8"));
                    logger.info(new String(response.getHeader(),"UTF-8"));
                    logger.info(response.getKey());
                    logger.info(String.valueOf(response.getMessageId()));
                    logger.info(String.valueOf(response.getType()));
                    logger.info(String.valueOf(response.isMessageIdRequired()));
                    logger.info(String.valueOf(response.isRetryable()));
                } catch (Exception e) {
                    e.printStackTrace();
                }


                logger.info("response:"+JSON.toJSONString(asyncActionToken.getResponse()));
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                logger.info("onFailure");
                exception.printStackTrace();
            }
        });*/
    }
}
